package org.colin.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.colin.aop.annotation.LogAnnotation;
import org.colin.code.BaseResponseCode;
import org.colin.config.ConfigCode;
import org.colin.dictPojo.DEntry;
import org.colin.dictPojo.DEntryEnh;
import org.colin.service.DEntryService;
import org.colin.utils.DataResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 词条表 前端控制器
 * </p>
 *
 * @author LiRuiFeng
 * @since 2021-01-09
 */
@Controller
@Api(tags = "基础数据管理-词条管理")
@RequestMapping("/dEntry")
public class DEntryController {


//    @Autowired
//    RedisTemplate redisTemplate;

    @Autowired
    DEntryService dEntryService;

    @ApiOperation(value = "新增词条接口")
    @LogAnnotation(title = "基础数据管理", action = "新增词条接口")
//    @ApiIgnore
    @PostMapping("addEntry")
    @ResponseBody
    public DataResult addEntry(@ApiParam(name="词条类",value="词条对象",required=true)  DEntryEnh dEntry  ){
        String prefix = dEntry.getPrefix();
        if(prefix == null || prefix.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败,词条名称不能为空");
        }
        Integer language = dEntry.getLanguage();
        if(language == null || language <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败,语种不能为空");
        }


        String imageLists = dEntry.getImageLists();
        List imageList = null;
        if(imageLists != null && !imageLists.trim().equals("")){
            imageList = JSONArray.parseArray(imageLists);
        }
        String videoLists = dEntry.getVideoLists();
        List videoList = null;
        if(videoLists != null && !videoLists.trim().equals("")){
            videoList = JSONArray.parseArray(videoLists);
        }
        String phraseLists = dEntry.getPhraseLists();
        List phraseList = null;
        if(phraseLists != null && !phraseLists.trim().equals("")){
            phraseList = JSONArray.parseArray(phraseLists);
        }

        DEntry dEntry1 = JSONObject.parseObject(JSONObject.toJSONString(dEntry)).toJavaObject(DEntry.class);
        boolean save = dEntryService.addEntry(dEntry1 , videoList , imageList ,phraseList );
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),"保存失败");
        }
    }


    @ApiOperation(value = "修改词条接口")
    @PutMapping("updateEntry")
//    @ApiIgnore
    @LogAnnotation(title = "基础数据管理", action = "修改词条接口")
    @ResponseBody
    public DataResult updateEntry(@ApiParam(name="词条类",value="词条对象",required=true)DEntryEnh dEntry ){
        HashMap<String, Object> hashMap = new HashMap<>();

        Integer id = dEntry.getId();
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "修改失败,唯一标识不能为空");
        }
        String prefix = dEntry.getPrefix();
        if(prefix == null || prefix.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "修改失败,词条名称不能为空");
        }


        String imageLists = dEntry.getImageLists();
        List imageList = null;
        if(imageLists != null && !imageLists.trim().equals("")){
            imageList = JSONArray.parseArray(imageLists);
        }
        String videoLists = dEntry.getVideoLists();
        List videoList = null;
        if(videoLists != null && !videoLists.trim().equals("")){
            videoList = JSONArray.parseArray(videoLists);
        }
        String phraseLists = dEntry.getPhraseLists();
        List phraseList = null;
        if(phraseLists != null && !phraseLists.trim().equals("")){
            phraseList = JSONArray.parseArray(phraseLists);
        }

        DEntry dEntry1 = JSONObject.parseObject(JSONObject.toJSONString(dEntry)).toJavaObject(DEntry.class);
        boolean save = dEntryService.updateEntry(dEntry1 , videoList , imageList , phraseList);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(), "修改失败");
        }
    }



    @LogAnnotation(title = "基础数据管理", action = "批量删除词条接口")
//    @ApiIgnore
    @DeleteMapping("deleteEntry")
    @ResponseBody
    public DataResult deleteEntry(@ApiParam(name="id",value="[1,2,3]",required=true)   String id ,@ApiParam(name="language",value="语种",required=true)@RequestParam("language") Integer language ){
        List ids = JSONArray.parseArray(id);
        if(ids == null || ids.size() <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "删除失败,唯一标识不能为空");
        }
        if(language == null || language.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "删除失败,语种不能为空");
        }
        boolean save = dEntryService.deleteEntry(ids,language);
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "删除失败");
        }
    }


    @ApiOperation(value = "查询词条详情")
    @LogAnnotation(title = "基础数据管理", action = "查询词条详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "唯一标识",required=true, paramType = "query"),
            @ApiImplicitParam(name="language",value="词条",required=true, paramType = "query"),
    })
    @GetMapping("selectEntryById")
    @ResponseBody
    public DataResult selectEntryById(Integer id , Integer language  ){
        if(id == null || id.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "查询失败,唯一标识不能为空");
        }
        if(language == null || language.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "查询失败,语种不能为空");
        }
        HashMap byId = dEntryService.selectEntryById(id , language);
        DataResult success = DataResult.success();
        success.setData(byId);
        return success;
    }



    @ApiOperation(value = "查询词条-分页")
    @LogAnnotation(title = "基础数据管理", action = "查询词条-分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "param" ,value = "文本", paramType = "query"),
            @ApiImplicitParam(name = "strokes" ,value = "笔画", paramType = "query"),
            @ApiImplicitParam(name = "hsk" ,value = "等级", paramType = "query"),
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query"),
            @ApiImplicitParam(name = "convType" ,value = "类型", paramType = "query"),
            @ApiImplicitParam(name = "page" ,value = "当前页",required=true, paramType = "query"),
            @ApiImplicitParam(name ="pageSize",value="每页数量",required=true, paramType = "query"),
    })
    @GetMapping("selectEntryIsPage")
    @ResponseBody
    public DataResult selectEntryIsPage(String param ,Integer strokes ,Integer hsk ,Integer language ,Integer convType ,Integer page , Integer pageSize ){
        Page<HashMap> pageE = new Page<>(page, pageSize);
        IPage<HashMap> list = dEntryService.selectEntryIsPage(pageE , param , strokes , hsk , language , convType);
        DataResult success = DataResult.success();
        success.setData(list);
        return success;
    }





    @ApiOperation(value = "批量修改词条状态")
    @LogAnnotation(title = "基础数据管理", action = "批量修改词条状态")
    @DeleteMapping("updateEntryByAppr")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "唯一标识", paramType = "query"),
            @ApiImplicitParam(name = "appr" ,value = "审核状态  2： 审核通过  3：审核未通过", paramType = "query"),
            @ApiImplicitParam(name = "releaseState" ,value = "发布状态   1：发布  2：未发布 ", paramType = "query"),
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query")
    })
    public DataResult updateEntryByAppr( @RequestBody @Valid String id , Integer language , Integer appr ,Integer releaseState  ){
        List ids = JSONArray.parseArray(id);
        if(ids == null || ids.size() <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,唯一标识不能为空");
        }
        if(language == null || language.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,语种不能为空");
        }

        if( (appr == null || appr < 0 ) && (releaseState == null || releaseState < 0 ) ){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,状态不能为空");
        }
        boolean save = dEntryService.updateEntryByAppr(ids,language  ,appr , releaseState );
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败");
        }
    }



    @ApiOperation(value = "批量修改词条等级")
    @LogAnnotation(title = "基础数据管理", action = "批量修改词条等级")
    @DeleteMapping("updateBatchEntryHsk")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id" ,value = "唯一标识", paramType = "query"),
            @ApiImplicitParam(name = "hsk" ,value = "hsk等级 ", paramType = "query"),
            @ApiImplicitParam(name = "language" ,value = "语种", paramType = "query")
    })
    public DataResult updateBatchEntryHsk(@RequestBody @Valid String id , Integer language ,Integer hsk   ){
        List ids = JSONArray.parseArray(id);
        if(ids == null || ids.size() <= 0){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,唯一标识不能为空");
        }
        if(language == null || language.equals("")){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),  "修改失败,语种不能为空");
        }

        if( hsk == null || hsk < 0 ){
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败,修改等级不能为空");
        }
        boolean save = dEntryService.updateBatchEntryHsk(ids,language  ,hsk );
        if(save == true){
            return DataResult.success();
        }else{
            return new DataResult(BaseResponseCode.OPERATION_ERROR.getCode(),   "修改失败");
        }
    }



}

